<?php

namespace p5\objects;

class CharsetUtils
{

    static public function to_utf8_name($encode)
    {
        $encode = strtolower($encode);
        $encode = $encode == "utf8" ? "utf-8" : $encode;
        return $encode;
    }


    static public function get_unicode_from_one_utf8($word)
    {
        $str = base64_encode($word);
        $str = str_replace("+", "", $str);
        $str = str_replace("=", "", $str);
        $str = str_replace("\\", "", $str);
        $str = str_replace("/", "", $str);
        return $str;
    }


    static public function get_php_encoding()
    {
        return "utf-8";
    }

    static public function get_os_encoding()
    {
        global $configs;
        $key = "os_encoding";
        if (isset($configs) && array_key_exists($key, $configs)) {
            return $configs[$key];
        }
        if (static::isWindows()) {
            return "gbk";
        }
        return "utf-8";
    }

    static public function isWindows()
    {
        return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
    }

    static public function to_os_encoding($string)
    {
        if (static::get_os_encoding() != static::get_php_encoding()) {
            return iconv(static::get_php_encoding(), static::get_os_encoding(), $string);
        }
        return $string;
    }

    static public function to_php_encoding($string)
    {
        if (static::get_os_encoding() != static::get_php_encoding()) {
            return iconv(static::get_os_encoding(), static::get_php_encoding(), $string);
        }
        return $string;
    }

}